var socket = io.connect('http://localhost:8090');
var StoryPointer = window.StoryPointer || {};
var utils = StoryPointer.Utils;

StoryPointer.Jira = {};
StoryPointer.Client = (function (window, document) {

	    var selectedCard;

	    var sendScore = function(){
		    $("#sendScore").click(function(){
			    var name = $(".inputName").val();
			    var score = selectedCard;
			    var jira = StoryPointer.Jira;

			    var data = { "name": name, "score": score, "jira": jira };

			    if(utils.isDataValid(data)){
				    socket.emit('sendScore', data);
				    $(".errorBar").addClass("hide");
				    $("#error").text("");
			    } else {
				    $(".errorBar").removeClass("hide");
				    $("#error").text("There was an error with your data");
				    console.log(data);
			    }

			    return false;
		    });
    };

    var listenJira = function() {
        socket.on("sendJira", updateJira);
    };

    var updateJira = function(data) {
        StoryPointer.Jira = data.jira;
        $("#jira").text(data.jira);
		clearScore();
    };

	var clearScore = function() {
		$(".scoreCards li").removeClass("selected");
		selectedCard = "";
	};

	var selectScore = function() {
		$(".scoreCards li").click(function(){
			clearScore();
			$(this).addClass("selected");
			selectedCard = $(this).attr("data");
		});
	};

    var init = function(){
        $(function(){
            sendScore();
            listenJira();
	        selectScore();
        });
    };

    init();

})(window, document);

